package com.mytest.example.gobrs.task.chain4;

import com.gobrs.async.TaskSupport;
import com.gobrs.async.anno.Task;
import com.gobrs.async.task.AsyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Slf4j
@Task(retryCount = 3, failSubExec = true)
@Component
public class Chain4TaskB extends AsyncTask<Chain4Param1, String> {
    @Override
    public void prepare(Chain4Param1 o) {

    }

    @Override
    public String task(Chain4Param1 o, TaskSupport taskSupport) {
        log.info("Chain4TaskB cancel={},shouldThrow={}", o.getCancel(), o.getShouldThrow());
        if (o.getShouldThrow()) {
            throw new RuntimeException("Chain4TaskB发生异常");
        }
        return "Chain4TaskB";
    }

    @Override
    public boolean nessary(Chain4Param1 o, TaskSupport taskSupport) {
        if (o.getCancel()) {
            log.info("Chain4TaskB 跳过任务");
            return false;
        }
        return true;
    }

    @Override
    public void onSuccess(TaskSupport taskSupport) {
        log.info("Chain4TaskB 执行完成");

    }

    @Override
    public void onFail(TaskSupport taskSupport) {
        //在任务异常时发送告警信息
        log.error("Chain4TaskB 执行失败,ex={}", taskSupport.getResultMap().get(this.getClass()).getEx().getMessage());

    }
}
